package com.rjkj.medicalwaste.utils;


import com.rjkj.medicalwaste.bean.Decoder03Bean;
import com.rjkj.medicalwaste.bean.Decoder04Bean;

import java.text.DecimalFormat;

/**
 * 设备解码器
 */
public class Decoder {

    public static Decoder03Bean decoder03(String decoderdata) {
        Decoder03Bean decoder03Bean = new Decoder03Bean();
        decoder03Bean.setResult(decoderdata);
        return decoder03Bean;
    }

    public static boolean decoder81(String decoderdata) {
        return "01".equals(decoderdata.substring(6, 8));
    }

    public static Decoder04Bean decoder04(String decoderdata) {
        Decoder04Bean decoder04 = new Decoder04Bean();
        //称重
        DecimalFormat df = new DecimalFormat("0.00");
        try {
            int weight = (int) Long.parseLong(decoderdata.substring(6, 14), 16);
            String tmpWeight = df.format((float) (weight) / 1000);
            decoder04.setWeight(tmpWeight);
            decoder04.setWeightUnitStr(tmpWeight + "KG");
        } catch (Exception e) {
            decoder04.setWeight("0");
            e.printStackTrace();
        }
        try {
            //秤状态
            if ("01".equals(decoderdata.substring(14, 16))) {
                decoder04.setScales("出秤");
            } else if ("00".equals(decoderdata.substring(14, 16))) {
                decoder04.setScales("入秤");
            } else {
                decoder04.setScales("秤异常");
            }
        } catch (Exception e) {
            decoder04.setScales("0");
            e.printStackTrace();
        }

//        try {
//            //电池电压
//            df = new DecimalFormat("0.0");
//            String system_voltage = SPUtils.getSharedStringData(MyApplication.getInstance(), ParamsConstant.SYSTEM_VOLTAGE);
//            decoder04.setVoltage(df.format((float) Integer.valueOf(decoderdata.substring(16, 20), 16) / 10 + Float.parseFloat(system_voltage)));
//        } catch (Exception e) {
//            decoder04.setVoltage("0");
//            e.printStackTrace();
//        }
//        try { //温度
//            decoder04.setTemperature(df.format((float) Integer.valueOf(decoderdata.substring(20, 24), 16) / 10));
//        } catch (Exception e) {
//            decoder04.setTemperature("0");
//            e.printStackTrace();
//        }


        return decoder04;
    }


}
